package com.leased.handler;

import com.leased.exception.DatabaseException;
import com.leased.exception.YCException;
import com.leased.pojo.ClientUpdateInfo;
import com.leased.request.client.ClientUpdateRequest;
import com.leased.service.ClientUpdateService;
import com.leased.utils.Page;
import com.leased.utils.Validator;
import com.leased.utils.YCSystemStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by shencx on 2017/8/16.
 */
@Service
public class ClientUpdateHandler {

    Logger LOG = LoggerFactory.getLogger(ClientUpdateHandler.class);

    @Resource
    private ClientUpdateService clientUpdateService;

    @Value("${download.file.url}")
    private String downloadUrl;

    /**
     * 添加版本更新信息
     * @param clientUpdateInfo
     * @throws YCException
     */
    public void addClientUpdate(ClientUpdateInfo clientUpdateInfo) throws YCException {
        /** 参数校验 */
        Validator.isEmpty(clientUpdateInfo, YCSystemStatusEnum.PARAM_EMPTY);
        Validator.isEmpty(clientUpdateInfo.getVersionName(),"版本名称不能为空");
        Validator.isEmpty(clientUpdateInfo.getPackageAddress(),"上传包地址不能为空");
        clientUpdateInfo.setPackageAddress(downloadUrl+clientUpdateInfo.getPackageAddress());
        try {
            if (clientUpdateInfo.getId() == null) {
                clientUpdateService.addClientUpdateInfo(clientUpdateInfo);
            }else {
                clientUpdateService.updateClientInfo(clientUpdateInfo);
            }
        }catch (DatabaseException e) {
            LOG.error("addClientUpdate exception",clientUpdateInfo);
            throw new YCException(YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getCode(), YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getDesc());
        }
    }

    /**
     * 获取版本信息列表
     * @param clientUpdateRequest
     * @return
     * @throws YCException
     */
    public Page<ClientUpdateInfo> getClientUpdateInfoPage(ClientUpdateRequest clientUpdateRequest) throws YCException {
        Page<ClientUpdateInfo> page = null;
        try {
            page = clientUpdateService.fetchClientUpdateList(clientUpdateRequest);
            return page;
        } catch (DatabaseException e) {
            LOG.error("getClientUpdateInfoPage exception",clientUpdateRequest);
            throw new YCException(YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getCode(), YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getDesc());
        }
    }

    /**
     * 获取版本详情
     * @param id
     * @return
     * @throws YCException
     */
    public ClientUpdateInfo getClientUpdateDetail(Integer id) throws YCException {
        /** 参数校验 */
        Validator.isEmpty(id,"主键ID不能为空");
        ClientUpdateInfo clientUpdateInfo = null;
        try {
            clientUpdateInfo = clientUpdateService.fetchClientUpdateDetail(id);
            clientUpdateInfo.setPackageAddress(clientUpdateInfo.getPackageAddress().substring(clientUpdateInfo.getPackageAddress().lastIndexOf("/")+1));
            return clientUpdateInfo;
        }catch (DatabaseException e) {
            LOG.error("getClientUpdateDetail exception",id);
            throw new YCException(YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getCode(), YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getDesc());
        }
    }
}
